<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/css/style.css">
<!--<link href="--><?php //echo URL::base() ?><!--media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">-->
<script src="<?php echo URL::base() ?>media/js/jquery-1.10.1.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.core.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.widget.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.position.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.autocomplete.min.js"></script>

<!--<link rel="stylesheet" href="--><?php //echo URL::base() ?><!--media/vendor/artdialog/skins/default.css" type="text/css"-->
<!--      media="all"/>-->
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<!--<link href="--><?php //echo URL::base() ?><!--media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">-->
<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>
<style type="text/css">
    .display_div {
        display: none
        }

    .uppercase {
        text-transform: uppercase;
        }
    /*.table tbody tr th {*/
        /*color: #333333;*/
        /*font-weight: normal;*/
        /*border: 1px solid #ffffff;*/
        /*}*/

    /*.table tfoot tr {*/
        /*background-color: #eeeeee;*/
        /*}*/

    /*.table tfoot tr td {*/
        /*border: 1px solid #ffffff;*/
        /*text-align: center;*/
        /*}*/

    /*.table thead tr th {*/
        /*text-align: center;*/
        /*}*/

    /*table tbody tr:nth-child(2n) {*/
        /*background-color: #f5f5f5;*/
        /*}*/
</style>
<!-- 查询 -->
    <form class="form-horizontal" action="<?php echo URL::site() ?>recieve/detail/select" method="get" id="myfrom">
        <div class="control-group">
            <label class="control-label">收货时间从:</label>
            <div class="controls">
                <input readonly="readonly" class="input-medium datepicker" type="text"
                       value="<?php if (isset($_GET['date_from'])) {
                           echo $_GET['date_from'];
                       } ?>" name='date_from' id="date_from"/>
                到 <input readonly="readonly" class="input-medium datepicker" type="text"
                         value="<?php if (isset($_GET['date_to'])) {
                             echo $_GET['date_to'];
                         } ?>" name='date_to' id="date_to"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">SKU:</label>
            <div class="controls">
                <input type="text" class="uppercase input2" name="sku" id="sku" value="<?php echo $_GET['sku'];?>">
            </div>
        </div>
        <div class="control-group">
           <label class="control-label">供应商:</label>
           <div class="controls">
        	 	<input type="hidden" class="input-mini" name="supplier_id" id="supplier_id" value="<?php echo $_GET['supplier_id'];?>" autocomplete="off"
                       readonly="readonly"/>
                <input type="text" class="input2" name="supplier_name" id="supplier_name"
                	value="<?php echo $_GET['supplier_name'];?>" placeholder="请输入供应商" autocomplete="off"/>
        	</div>
        </div>
        <div class="control-group">
            <label class="control-label">主体:</label>
            <div class="controls">
                <select name="enterprise_dominant" id="enterprise_dominant" class="input2">
                    <option value="-1">全部</option>
                <?php $body_arr = Kohana::$config->load('enterprise_dominant')->as_array();?>
                <?php foreach ($body_arr as $k=>$v){?>
                    <option value="<?php echo $k;?>" 
                        <?php if(isset($_GET['enterprise_dominant']) && $k==$_GET['enterprise_dominant'])
                            echo 'selected';?>>
                        <?php echo $v;?>
                    </option>
                <?php }?>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">是否出口退税:</label>
            <div class="controls">
                <select name="export_tax_rebate" id="export_tax_rebate" class="input2">
                    <option value="-1">请选择</option>
                    <option value="1" <?php if(isset($_GET['export_tax_rebate']) && 1==$_GET['export_tax_rebate'])echo 'selected';?>>是</option>
                    <option value="0" <?php if(isset($_GET['export_tax_rebate']) && 0==$_GET['export_tax_rebate'])echo 'selected';?>>否</option>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">计划员:</label>
            <div class="controls">
                <input class="input2" type="text" name="purchaser_name" value="<?php echo $_GET['purchaser_name'];?>" id="purchaser_name">
<!--                <input type="hidden" name="per_page" value="50" id="per_page">-->
<!--                <input type="hidden" name="current_page" value="0" id="current_page">-->
            </div>
        </div>
         <div class="control-group">
             <div class="controls">
                <button type="submit" id="search_btn_not" class="btn btn-info">查询</button>
                <button type="reset" class="btn" onclick="resetDate()">重置</button>
                <button type="submit" id="search_load_not" name="down" value="1" class="btn">下载</button>
             </div>
        </div>
</form>

<!-- 查询结果 -->

<table class="table table-bordered" id="table_content" style="border-collapse:collapse;">
<thead>
    <tr>
        <th style="width:30px;">序号</th>
        <th style="width:90px;">收货日期</th>
        <th style="width:90px;">收货明细单号</th>
        <th style="width:90px;">VMI预约单号</th>
            <th>供应商</th>
            <th>是否退税</th>
        <th>SKU</th>
        <th>中文名称</th>
            <th>仓库</th>
            <th>主体</th>
        <th style="min-width:30px;">规格</th>
        <th style="min-width:30px;">储位</th>
        <th style="width:50px;">计划员</th>
        <th style="width:50px;">采购数</th>
        <th style="width:50px;">到货数</th>
    </tr>
</thead>
<tbody id="div_content_not">
<?php
if(isset($receieve_lists) && $receieve_lists){
    foreach ($receieve_lists as $num => $receieve_list){?>
        <tr>
            <td><?php echo $num+1+$current_page ?></td>
            <td><?php echo $receieve_list['recieve_time'] ?></td>
            <td><?php echo $receieve_list['id'] ?></td>
            <td><?php echo $receieve_list['poid'] ?></td>
            <td> <?php echo supplierskus::getNameById($receieve_list['supplier']);?></td>
            <td><?php if($receieve_list['export_tax_rebate']==0){
                    echo '非出口退税';
                }else{
                    echo '出口退税';
                } ;?></td>
            <td><?php echo $receieve_list['sku'] ?></td>
            <td><?php echo $receieve_list['sku_name'] ?></td>
            <td><?php
                $storeName = '';
                $stores = Kohana::$config->load('vmi_warehouse')->as_array();
                isset($stores[$receieve_list['store']]) && $storeName = $stores[$receieve_list['store']];
                echo $storeName;
                ?></td>
            <td><?php
                $body = '';
                $body_arr = Kohana::$config->load('enterprise_dominant')->as_array();
                isset($body_arr[$receieve_list['enterprise_dominant']]) && $body = $body_arr[$receieve_list['enterprise_dominant']];
                echo $body;
                ?></td>
            <td><?php echo $receieve_list['sku_standard'] ?></td>
            <td><?php echo $receieve_list['storage_position'] ?></td>
            <td><?php echo Erpuser::nick_by_id($receieve_list['purchase_id']); ?></td>
            <td><?php echo $receieve_list['purchase_quantity'] ?></td>
            <td><?php echo $receieve_list['arrival_quantity'] ?></td>
        </tr>
    <?php }?>
<?php }?>
</tbody>
<!--<tfoot>-->
<!--    <td>合计</td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td></td>-->
<!--    <td><span id="sum_arrival_quantity">0</span></td>-->
<!--</tfoot>-->
</table>

<div>
    <?php if (isset($pagination)) {
        echo $pagination;
    } ?>
</div>
<div><span></span></div>
<script type="text/javascript">
    $(document).ready(function () {
        laydate.render({
            elem: '#date_from',
        });
        laydate.render({
            elem: '#date_to',
        });
        $("#supplier_id").keyup(function (e) {
            var $val = $(this).val();
            showHint($val);
        });
    });
    
    function showHint(str) {
        if (str.length == 0) {
            $("#supplier_id").val('');
            $("#txtHint").hide();
            return false;
        }
        var url = "<?php echo URL::base() ?>purchase/get_supplier";
        url += "?term=" + str;
        url += "&sid=" + Math.random();
        $.getJSON(url, function (data) {
            $("#txtHint").html("");
            if(data.length){
                $.each(data, function (index, value) {
                    $('<li>' + value.name + '</li>').appendTo($("#txtHint"));
                    $("#txtHint").show();
                    $('#txtHint li').click(function () {
                        $("#supplier_id").val($(this).text());
                        $(this).parent().hide();
                    });
                });
            }
        });
    }

    $(window).scroll(function () {
        //此方法是在滚动条滚动时发生的函数
        // 当滚动到最底部以上100像素时，加载新内容
        var $doc_height, $s_top, $now_height;
        $doc_height = $(document).height();        //这里是document的整个高度
        $s_top = $(this).scrollTop();            //当前滚动条离最顶上多少高度
        $now_height = $(this).height();            //这里的this 也是就是window对象
      //  if (($doc_height - $s_top - $now_height) < 100) get_add();
    });

    $("#search_btn").click(function () {
        empty_table();
        var details = get_search_data();
        var page = get_pagination();

        //查询当前结果
        var re = get_ajax($.parseJSON('{' + details + ',' + page + '}'));

        if (re) {
            append_table(re);
        }

        //查询SUM结果
        var sum = get_ajax_sum($.parseJSON('{' + details + '}'))

        if (sum) {
            $("#sum_ids").text(sum.sum_ids);
            $("#sum_arrival_quantity").text(sum.sum_arrival_quantity);
        }

        if (re && sum) {
            set_pagination_next();
            get_uri();
        }
        show_more_btn();
        show_search_load();

    });

    uri = "";

    //查询条件
    function get_search_data() {
        var store = $("#store").val();
        var recieve_time_from = $.trim($("#recieve_time_from").val());
        var recieve_time_to = $.trim($("#recieve_time_to").val());
        var sku = $.trim($("#sku").val());
        var supplier_name = $.trim($("#supplier_name").val());
        var enterprise_dominant = $("#enterprise_dominant").val();
        var export_tax_rebate = $("#export_tax_rebate").val();
        var purchaser_name = $.trim($("#purchaser_name").val());
        var packaging = $("#packaging").val();

        return  '\"packaging\": \"' + packaging + '\",'+
             '\"store\": \"' + store + '\",' +
            '\"recieve_time_from\": \"' + recieve_time_from + '\",' +
            '\"recieve_time_to\": \"' + recieve_time_to + '\",' +
            '\"sku\": \"' + sku + '\",' +
            '\"supplier_name\": \"' + supplier_name + '\",' +
            '\"enterprise_dominant\": \"' + enterprise_dominant + '\",' +
            '\"export_tax_rebate\": \"' + export_tax_rebate + '\",' +
            '\"purchaser_name\": \"' + purchaser_name + '\"';
// 			'\"order_by\": \"'+order_by+'\"';
    }

    //获取分页信息
    function get_pagination() {
        var per_page = $("#per_page").val();//每一页多少个
        var current_page = $("#current_page").val();//当前是那一页

        return '\"limit\": \"' + per_page + '\",' +
            '\"offset\": \"' + current_page + '\"';
    }

    function get_uri() {
        var store = $.trim($("#store").val());
        var recieve_time_from = $.trim($("#recieve_time_from").val());
        var recieve_time_to = $.trim($("#recieve_time_to").val());
        var sku = $.trim($("#sku").val());
        var supplier_name = $.trim($("#supplier_name").val());
        var enterprise_dominant = $.trim($("#enterprise_dominant").val());
        var export_tax_rebate = $.trim($("#export_tax_rebate").val());
        var purchaser_name = $.trim($("#purchaser_name").val());
        var packaging = $.trim($("#packaging").val());
        uri += "?store=" + store;
        uri += "&recieve_time_from=" + recieve_time_from;
        uri += "&recieve_time_to=" + recieve_time_to;
        uri += "&sku=" + sku;
        uri += "&supplier_name=" + supplier_name;
        uri += "&enterprise_dominant=" + enterprise_dominant;
        uri += "&export_tax_rebate=" + export_tax_rebate;
        uri += "&purchaser_name=" + purchaser_name;
        uri += "&packaging=" + packaging;
    }

    //显示查看更多
    function show_more_btn() {
        var sum_ids = parseInt($("#sum_ids").text());
        var per = parseInt($("#per_page").val());
        var current_page = parseInt($("#current_page").val());
        var left = sum_ids - current_page;
        $("#left_ids").text(left);

        if (left > 0) {
            $("#add_more").removeClass('display_div');
        }
        else {
            $("#add_more").addClass('display_div');
        }
    }

    //下载按钮
    function show_search_load() {
        var sum_ids = parseInt($("#sum_ids").text());
        if (sum_ids <= 0) {
            $("#search_load").addClass('display_div');
        }
        else {
            $("#search_load").removeClass('display_div');
        }
    }

    //追加表格
    function append_table(data) {
        $("#div_content").append(data);
    }

    //清空表格
    function empty_table() {
        uri = "";
        $("#current_page").val(0);
        $("#sum_ids").text(0);
        $("#div_content").empty();
    }

    //设置当前页的下一页
    function set_pagination_next() {
        $("#current_page").val(parseInt($("#current_page").val()) + parseInt($("#per_page").val()));
    }

    //获取查询结果
    function get_ajax(details) {
        var re = {};
        $.ajax({
            type: 'post',
            url: '<?php echo url::base(TRUE);?>recieve/detail/select',
            dataType: 'html',
            data: details,
            async: false,
            success: function (data) {
                re = data;
            },
            error: function () {

            }
        });

        return re;
    }

    //获取合计信息
    function get_ajax_sum(details) {
        var re = {};
        $.ajax({
            type: 'post',
            url: '<?php echo url::base(TRUE);?>recieve/detail/select_sum',
            dataType: 'json',
            data: details,
            async: false,
            success: function (data) {
                re = data;
            },
            error: function () {
            }
        });

        return re;
    }

    $("#add_more").click(function () {
        var details = get_search_data();
        var page = get_pagination();
        var re = get_ajax($.parseJSON('{' + details + ',' + page + '}'));
        if (re) {
            append_table(re);
            set_pagination_next();
            show_more_btn();
        }

    });

    //下载
    $("#search_load").click(function () {
        if (uri) {
            window.location.href = "<?php echo url::base(TRUE)?>recieve/detail/load" + uri;
        }
    });
    function resetDate() {
        $('.input-medium').val('');
        $('.input2').val('');
        $('.input-mini').val('');
        $('#export_tax_rebate').val('-1');
    }
</script>
